home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Examples / Text / Textension / Include / TextensionCommon.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-21  |  12.3 KB  |  459 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        TextensionCommon.h
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Written by:    Essam Zaky
  7.  
  8.     Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <2>      1/4/94    EZ        clean up
  13.          <1>      1/4/94    EZ        first checked in
  14.  
  15. */
  16.  
  17.  
  18. #ifndef _TextensionCommon_
  19. #define _TextensionCommon_
  20.  
  21. #ifndef _ToolBoxDump_
  22. #include "ToolBoxDump.h"
  23. #endif
  24.  
  25. #ifdef sysTextPerf
  26. #include <Timer.h>
  27. #endif
  28. //***************************************************************************************************
  29.  
  30. #if !defined(applec) || defined(__SC__)
  31. class SingleObject {
  32. };
  33. #endif
  34.  
  35. #ifdef __STDC__
  36. class HandleObject {
  37. };
  38. #if defined(applec) || defined(__SC__)
  39. #error "•••handle object redefined"
  40. #endif
  41. #endif
  42. //****************************************************
  43.  
  44.  
  45. typedef long* longPtr;
  46. typedef Fixed* FixedPtr;
  47. typedef short* shortPtr;
  48. typedef RGBColor* RGBColorPtr;
  49. typedef unsigned char uchar;
  50. typedef uchar* uPtr;
  51. typedef unsigned short ushort;
  52. typedef unsigned long ulong;
  53. //***************************************************************************************************
  54.  
  55.  
  56. const short kMaxInt = 0x7FFF;
  57. const long kMaxLongInt = 0x7FFFFFFF;
  58.  
  59. //Direction constants
  60. const char kRL = -1;
  61. const char kLR = 0;
  62. const char kSysDirection = 1;
  63. const char kLineDirection = 2; //returned only from non text styles
  64. //****************************************************
  65.  
  66. //Direction CaretDirection
  67. const char kRLCaret = -1;
  68. const char kLRCaret = 0;
  69. const char kRunDir = 1;
  70. //****************************************************
  71.  
  72. //const for sel state and objects hilite
  73. const char kSelOff = 0;
  74. const char kSelOn = 1;
  75. const char kSelInactiv = 2;
  76. //****************************************************
  77.  
  78. //special characters
  79.  
  80. const uchar kMaxCtrlChar = 0x1F; //•• used in TextensionUtil.a (GetCtrlCharOffset)
  81.  
  82. const uchar kRLArrowCharCode = 0x1C;
  83. const uchar kLRArrowCharCode = 0x1D;
  84. const uchar kUpArrowCharCode = 0x1E;
  85. const uchar kDownArrowCharCode = 0x1F;
  86.  
  87. const uchar kTabCharCode = 9;
  88. const uchar kCRCharCode = 0xD;
  89. const uchar kRomanSpaceCharCode = 0x20;
  90.  
  91. const uchar kClearCharCode = 0x1B;
  92. const uchar kBackSpaceCharCode = 8;
  93.  
  94. //****************************************************
  95.  
  96. typedef short TSize;
  97. //const for "sizeInfo" param to ITextension
  98. const TSize kSmallSize = 0;
  99. const TSize kMediumSize = 1;
  100. const TSize kLargeSize = 2;
  101. //****************************************************
  102.  
  103. struct TOffsetPair {
  104.     long firstOffset;
  105.     long lastOffset;
  106. };
  107. //****************************************************
  108.  
  109. //For run boundaries select problem(including script problems) ••• to be more documented
  110. /*trailEdge is to distinguish between for instance offset 5 as either the end of the fifth char or the start
  111. of the 6th char (this may be different in display if text is mixed LR and RL)
  112. */
  113. struct TOffset {
  114.     long          offset;
  115.     Boolean         trailEdge;
  116.     
  117.     TOffset() {}
  118.     
  119.     TOffset(long theOffset, Boolean trail = false) {
  120.         offset = theOffset;
  121.         trailEdge = trail;
  122.     }
  123.     
  124.     inline operator long() {return offset;}
  125.     
  126.     void Set(long theOffset, Boolean trail = false) {
  127.         offset = theOffset;
  128.         trailEdge = trail;
  129.     }
  130.  
  131.     Boolean operator==(const TOffset& other) const;
  132.     inline Boolean operator!=(const TOffset& other) const
  133.         {return !(*this == other);}
  134. };
  135. //****************************************************
  136.  
  137. struct TOffsetRange {
  138.     TOffset rangeStart;
  139.     TOffset rangeEnd;
  140.     
  141.     TOffsetRange() {}
  142.     
  143.     TOffsetRange(const TOffset& start, const TOffset& end);
  144.  
  145.     TOffsetRange(long offsetStart, long offsetEnd, Boolean trailStart = false, Boolean trailEnd = true);
  146.     
  147.     void Set(long offsetStart, long offsetEnd, Boolean trailStart, Boolean trailEnd);
  148.     
  149.     inline long Len() const {return rangeEnd.offset-rangeStart.offset;}
  150.     
  151.     long Start() const {return rangeStart.offset;}
  152.     long End() const {return rangeEnd.offset;}
  153.     Boolean IsEmpty() const {return rangeStart.offset == rangeEnd.offset;}
  154.     
  155.     Boolean operator==(const TOffsetRange& other) const;
  156.     inline Boolean operator!=(const TOffsetRange& other) const
  157.         {return !(*this == other);}
  158.     
  159.     void Offset(long value);
  160.     
  161.     void CheckBounds(); //swap range start, rangeEnd if necessary
  162. };
  163. //****************************************************
  164.  
  165.  
  166. struct TLineHiteInfo {
  167.     short    lineHite;
  168.     short    lineAscent;
  169. };
  170. //***************************************************************************************************
  171.  
  172. struct TTempReference {
  173.     void* reference;
  174.     char used;
  175. };
  176.  
  177. const kMaxTempReferences = 5;
  178.  
  179. class CTempReferences : private SingleObject {
  180.     public:
  181.         CTempReferences() {}
  182.         
  183.         void ITempReferences();
  184.         void Free();
  185.         
  186.         void* Get();
  187.         void Done(void* theRef);
  188.     
  189.         #ifdef txtnDebug
  190.         short Count();
  191.         void Clean();
  192.         #endif
  193.         
  194.     protected:
  195.         virtual void* CreateNewReference() = 0;
  196.         virtual void FreeReference(void* ref) = 0;
  197.         
  198.     private:
  199.         TTempReference fReferences[kMaxTempReferences];
  200. };
  201. //***************************************************************************************************
  202.  
  203. class CTempRegions : public CTempReferences {
  204.     public:
  205.         CTempRegions() {}
  206.         
  207.         inline void ITempRegions() {this->ITempReferences();}
  208.         
  209.         inline RgnHandle GetRegion() {return RgnHandle(this->Get());}
  210.         inline void DoneWithRegion(RgnHandle theRgn) {this->Done(theRgn);}
  211.     
  212.     protected:
  213.         virtual void* CreateNewReference();
  214.         virtual void FreeReference(void* ref);
  215.         
  216.     private:
  217. };
  218. //***************************************************************************************************
  219.  
  220. #ifdef sysTextPerf
  221. const short kPerfFormatCall = 0;
  222. const short kPerfPixel2CharCall = 1;
  223. const short kPerfChar2PixelCall = 2;
  224. const short kPerfDrawCall = 3;
  225.  
  226. const short kCountPerfCalls = 4;
  227.  
  228. class CSysTextPerformance : private SingleObject {
  229.     public :
  230.         CSysTextPerformance() {fCount = 0;}
  231.         
  232.         void StartCount();
  233.         void EndCount();
  234.  
  235.         inline char IsOn() {return fCount;}
  236.         
  237.         void PreCall(short callId);
  238.         void PostCall(short callId);
  239.         
  240.         inline long GetCounterValue(short callId) {return fCounters[callId];}
  241.         
  242.     private:
  243.         long fCounters[kCountPerfCalls];
  244.         char fCount;
  245.         TMTask fTimerTask;
  246.         short fSavedSR;
  247.         long fOverhead;
  248. };
  249. //***************************************************************************************************
  250. #endif
  251.  
  252. //••• Globals
  253. extern char gHasManyScripts;
  254. extern Boolean gSys2Directions;
  255. extern Boolean    g2Bytes;
  256.  
  257. #ifndef txtnSys7
  258. extern Boolean gSystem7OrNewer;
  259.  
  260. extern Boolean gUseScriptManager7; //testing purposes, allow to test old script manager routines
  261. #endif
  262.  
  263. extern Boolean gHasColor;
  264.  
  265. extern Boolean gTextensionImaging;
  266.  
  267. /*••this is declared as reference instead of as an automatic "CTempRegions gTempRegions;" since
  268. the power pc compiler has a problem in the later case (the virtual table of gTempRegions is not initialized
  269. . Actually, it's better to create it in the heap to reduce the global area */
  270. extern CTempRegions* gTempRegions;
  271.  
  272.  
  273. #ifdef sysTextPerf
  274. extern CSysTextPerformance gSysTextPerf;
  275. #endif
  276. //***************************************************************************************************
  277.  
  278. struct TxtnLongRect; //later
  279. //***************************************************************************************************
  280.  
  281. Handle AllocateTempHandle(Size countBytes, char lockIt, OSErr* err);
  282.  
  283. long GetSysScrapTypeSize(ResType theType);
  284.  
  285. inline long MaxLong(long long1, long long2) {return((long1 > long2) ? long1 : long2);}
  286. inline long MinLong(long long1, long long2) {return((long1 < long2) ? long1 : long2);}
  287.  
  288. long Clip(long val, long lowerBound, long upperBound);
  289. inline short ClipToInt(long val) {return short(Clip(val, -kMaxInt, kMaxInt));}
  290.  
  291. void SwapShort(short* short1, short* short2);
  292. void SwapLong(long* long1, long* long2);
  293.  
  294. inline short RectHite(const Rect theRect) {return (theRect.bottom - theRect.top);}
  295. inline short RectWidth(const Rect theRect) {return (theRect.right - theRect.left);}
  296.  
  297. Boolean IsColorPort();
  298.  
  299. short GetMaxPixDepth(const Rect& aRect);
  300.  
  301. void SetCursorIBeam();
  302.  
  303. /* •••
  304. This routine is here since since for instance in AIS : Font2Script returns 4 when font is roman and
  305. forceFont flag is on, the same problem should exist in other systems
  306. */
  307. short MonoFont2Script(short aFont);
  308. Boolean IsScript2Bytes(short theScript);
  309. inline Boolean IsScriptR2L(short theScript) {return GetScript(theScript, smScriptRight) != 0;}
  310.  
  311. inline short Script2DefaultFont(short scriptNo)
  312.     {return short(GetScript(scriptNo, smScriptAppFond));}
  313.  
  314. void MyKeyScript(short newScript);
  315.  
  316. Boolean DoScrollRect(const Rect* rect2Scroll, long hPix, long vPix, RgnHandle invalidRgn
  317.                                         , Boolean move = false);
  318.  
  319. long LongRoundUp(long aNumber, short aModulus);
  320. short ShortRoundUp(short aNumber, short aModulus);
  321.  
  322. #ifdef txtnScal
  323. short ScaleVal(short val2Scale, short numer, short denom, Boolean roundUp = true);
  324. long LongScaleVal(long val2Scale, short numer, short denom, Boolean roundUp = true);
  325. #endif
  326.  
  327. Fixed FixScaleVal(Fixed val2Scale, short numer, short denom);
  328.  
  329. #ifndef txtnRulers
  330. #ifdef txtnScal
  331. Fixed GetDefaultTabWidth(Fixed tabPixStart, short numer = 1, short denom = 1);
  332. #else
  333. Fixed GetDefaultTabWidth(Fixed tabPixStart);
  334. #endif
  335. #endif
  336.  
  337. inline Boolean IsCtrlChar(uchar theChar) {return theChar <= kMaxCtrlChar;}
  338.  
  339.  
  340.  
  341. //***************************************************************************************************
  342.  
  343. //•externals
  344.  
  345. extern pascal short GetCtrlCharOffset(uPtr chars, short len, unsigned char* theChar); //returns the offset of the char, -1 if not found
  346. extern pascal short SearchChar(uchar theChar, uPtr chars, short len); //returns the offset of the char from "chars", -1 if not found
  347. extern pascal short SearchCharBack(uchar theChar, uPtr chars, short lenBefore); //same as SearchChar but search backward
  348.  
  349. extern pascal void Add2ArrayElements(long value, char* startPtr, long count, short elemSize);
  350.  
  351. #ifndef powerc
  352. extern pascal void Add2LongArray(long value, char* startPtr, long count);
  353. #endif
  354.  
  355. extern "C" {
  356.     extern char CompareBytes(uPtr ptr1, uPtr ptr2, long count);
  357.     //••only the short part is used from "count". declared as long since the compiler pushes 4 bytes when "extern C"
  358. }
  359.  
  360. //***************************************************************************************************
  361.  
  362. struct LongPoint {
  363. //the order of v and h is the same as the toolbox and VPoint in MacApp, don't change
  364.     long v;
  365.     long h;
  366.     
  367.     //••LongPoint members should not move memory, they can be members of Handle objects
  368.     LongPoint() {}
  369.     
  370.     LongPoint(long horiz, long ver) {
  371.         h = horiz;
  372.         v = ver;
  373.     }
  374.  
  375.     LongPoint(Point shortPt) {
  376.         h = shortPt.h;
  377.         v = shortPt.v;
  378.     }
  379.     
  380.     inline void Set(long horiz, long ver) {
  381.         h = horiz;
  382.         v = ver;
  383.     }
  384.     
  385.     inline Boolean operator==(const LongPoint& aPoint) const {return (h == aPoint.h) && (v == aPoint.v);}
  386.     inline Boolean operator!=(const LongPoint& aPoint) const {return (h != aPoint.h) || (v != aPoint.v);}
  387. };
  388. //***************************************************************************************************
  389.  
  390.  
  391. struct TxtnLongRect {
  392. //the order top, left, bottom, right is the same as the toolbox and VRect in MacApp, don't change
  393.     long top;
  394.     long left;
  395.     long bottom;
  396.     long right;
  397.     
  398.     //••TxtnLongRect members should not move memory, they can be members of Handle objects
  399.     
  400.     inline TxtnLongRect(long l, long t, long r, long b) {
  401.         left = l;
  402.         top = t;
  403.         right = r;
  404.         bottom = b;
  405.     }
  406.     
  407.     inline TxtnLongRect() {}
  408.     
  409.     inline TxtnLongRect(Rect shortRect) { //implicit conversion from Rect to TxtnLongRect
  410.         left = shortRect.left;
  411.         top = shortRect.top;
  412.         right = shortRect.right;
  413.         bottom = shortRect.bottom;
  414.     }
  415.  
  416.     inline void Set(long l, long t, long r, long b) {
  417.         left = l;
  418.         top = t;
  419.         right = r;
  420.         bottom = b;
  421.     }
  422.     
  423.     inline long Hite() const {return bottom-top;}
  424.     inline long Width() const {return right-left;}
  425.  
  426.     Boolean Sect(const TxtnLongRect* src, TxtnLongRect* intersection) const;
  427.  
  428.     Boolean IsPointInside(const LongPoint* thePt) const;
  429.     
  430.     void Inset(long h, long v);
  431.     
  432.     void Offset(long h, long v);
  433.     
  434.     Boolean operator==(const TxtnLongRect& aRect) const;
  435.     inline Boolean operator!=(const TxtnLongRect& aRect) const
  436.         {return !(*this == aRect);}
  437. };
  438. //***************************************************************************************************
  439.  
  440.  
  441. inline Boolean IsRectRgn(RgnHandle aRgn) {return (*aRgn)->rgnSize == 10;}
  442.  
  443. Boolean ClipFurther(Rect* moreClipRect, RgnHandle origClip);
  444. Boolean CalcClipRect(Rect* theRect);
  445.  
  446. void InvalSectRect(Rect* theRect, RgnHandle theClip = nil);
  447.  
  448. //***************************************************************************************************
  449.  
  450.  
  451. #ifdef txtnDebug
  452. #define TX_DebugMessage(message) DebugStr(message)
  453. #else
  454. #define TX_DebugMessage(message)
  455. #endif
  456. //***************************************************************************************************
  457.  
  458. #endif
  459.